import requests
from typing import Optional

class GitlabProject:
    def __init__(self, name: str):
        self.name = name

    def get_name(self) -> str:
        return self.name


def get_gitlab_projects(token: str, base_url: str, search: Optional[str] = None, id: Optional[str] = None) -> list[GitlabProject]:
    headers = {
        'Authorization': f'Bearer {token}'
    }

    if id:
        url = f"{base_url}/projects/{id}"
    else:
        url = f"{base_url}/projects?per_page=100&page=1"
        if search:
            url += f"&search={search}"

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        data = response.json()
        # List of projects
        projects = [GitlabProject(item['name']) for item in data if 'name' in item]
        return projects
    else:
        return []  # Return an empty list if the request fails
